package com.lmiot.xyclick.Thread;

import com.lmiot.xyclick.App.MyApp;
import com.lmiot.xyclick.Bean.DoAutoBean;
import com.lmiot.xyclick.Bean.SQL.ActionBean;
import com.lmiot.xyclick.Bean.SQL.ActionBeanSqlUtil;
import com.lmiot.xyclick.Bean.SQL.AutoBean;
import com.lmiot.xyclick.Method.DoActionUtils;
import com.lmiot.xyclick.Util.DataUtil;
import com.lmiot.xyclick.Util.LkxLog;
import com.lmiot.xyclick.Util.LogUtil;
import com.lmiot.xyclick.Util.RandomUtil;
import com.xiaoyi.intentsdklibrary.SDK.SDK;
import com.yhao.floatwindow.FloatUtil;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import org.greenrobot.eventbus.EventBus;

public class AutoThread extends Thread {
    public boolean exit = false;
    private final Object lock = new Object();
    private ActionBean mActionBean;
    private AutoBean mAutoBean;
    private int mStartActionNum;
    public boolean pause = false;

    public AutoThread(String str, AutoBean autoBean, ActionBean actionBean, int i) {
        setName(str);
        this.mAutoBean = autoBean;
        this.mActionBean = actionBean;
        this.mStartActionNum = i;
    }

    public void stopThread() {
        LogUtil.d("BaseThread", getName() + ":暂停");
        this.exit = true;
    }

    public void pauseThread() {
        LogUtil.d("BaseThread", getName() + ":暂停");
        this.pause = true;
    }

    public void resumeThread() {
        LogUtil.d("BaseThread", getName() + ":恢复");
        this.pause = false;
        synchronized (this.lock) {
            this.lock.notify();
        }
    }

    public void onPause() {
        synchronized (this.lock) {
            try {
                this.lock.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public void run() {
        super.run();
        if (getName().equals("auto")) {
            int repeatNum = this.mAutoBean.getRepeatNum();
            if (repeatNum == 0) {
                repeatNum = 1;
            }
            if (this.mAutoBean.getIsRandomRepeat()) {
                repeatNum = RandomUtil.getRandomNum(this.mAutoBean.getRandomMin(), this.mAutoBean.getRandomMax());
            }
            List<ActionBean> searchByID = ActionBeanSqlUtil.getInstance().searchByID(this.mAutoBean.getAutoID());
            sortList(searchByID);
            SDK.allNum = searchByID.size();
            SDK.nowNum = 1;
            if (repeatNum == -1) {
                while (!this.exit) {
                    if (this.pause) {
                        onPause();
                    }
                    int i = 0;
                    while (true) {
                        try {
                            if (i >= searchByID.size()) {
                                break;
                            }
                            SDK.nowNum = i;
                            if (this.exit) {
                                break;
                            }
                            if (this.pause) {
                                onPause();
                            }
                            try {
                                ActionBean actionBean = searchByID.get(i);
                                if (!SDK.isRunning) {
                                    break;
                                }
                                int delay = actionBean.getDelay();
                                if (i >= this.mStartActionNum) {
                                    DoActionUtils.doAction(this, actionBean);
                                    if (!actionBean.getEnable()) {
                                        Thread.sleep(10);
                                    } else if (FloatUtil.getAutoMs(MyApp.getContext())) {
                                        Thread.sleep((long) delay);
                                    } else {
                                        Thread.sleep((long) (delay * 1000));
                                    }
                                } else {
                                    Thread.sleep(500);
                                }
                                i++;
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        } catch (Exception e2) {
                            e2.printStackTrace();
                        }
                    }
                    try {
                        Thread.sleep(1);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    MyApp.mFinishNumRun++;
                }
            } else {
                for (int i2 = 0; i2 < repeatNum && !this.exit; i2++) {
                    if (this.pause) {
                        onPause();
                    }
                    int i3 = 0;
                    LkxLog.d("执行动作：earchByID.size()："+searchByID.size());
                    while (true) {
                        try {
                            if (i3 >= searchByID.size()) {
                                break;
                            }
                            SDK.nowNum = i3;
                            if (this.exit) {
                                break;
                            }
                            if (this.pause) {
                                onPause();
                            }
                            try {
                                ActionBean actionBean2 = searchByID.get(i3);
                                if (!SDK.isRunning) {
                                    break;
                                }
                                int delay2 = actionBean2.getDelay();
                                if (i3 >= this.mStartActionNum) {
                                    LkxLog.d("执行动作：mStartActionNum："+mStartActionNum+"，i3："+i3);
                                    DoActionUtils.doAction(this, actionBean2);
                                    if (!actionBean2.getEnable()) {
                                        Thread.sleep(10);
                                    } else if (FloatUtil.getAutoMs(MyApp.getContext())) {
                                        Thread.sleep((long) delay2);
                                    } else {
                                        Thread.sleep((long) (delay2 * 1000));
                                    }
                                } else {
                                    Thread.sleep(500);
                                }
                                i3++;
                            } catch (Exception e3) {
                                e3.printStackTrace();
                            }
                        } catch (Exception e4) {
                            e4.printStackTrace();
                        }
                    }
                    try {
                        Thread.sleep(1);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    MyApp.mFinishNumRun++;
                    if (MyApp.mFinishNumRun >= repeatNum) {
                        break;
                    }
                }
            }
            if (!this.exit) {
                EventBus.getDefault().post(new DoAutoBean(104, null, null, 0));
            }
        } else if (getName().equals("actionlist")) {
            List<ActionBean> list = DataUtil.nowActionBeanList;
            sortList(list);
            SDK.allNum = list.size();
            SDK.nowNum = 1;
            while (!this.exit) {
                if (this.pause) {
                    onPause();
                }
                int i4 = 0;
                while (true) {
                    try {
                        if (i4 >= list.size()) {
                            break;
                        }
                        SDK.nowNum = i4;
                        if (this.exit) {
                            break;
                        }
                        if (this.pause) {
                            onPause();
                        }
                        try {
                            ActionBean actionBean3 = list.get(i4);
                            if (!SDK.isRunning) {
                                break;
                            }
                            int delay3 = actionBean3.getDelay();
                            DoActionUtils.doAction(this, actionBean3);
                            Thread.sleep((long) delay3);
                            i4++;
                        } catch (Exception e5) {
                            e5.printStackTrace();
                        }
                    } catch (Exception e6) {
                        e6.printStackTrace();
                    }
                }
                try {
                    Thread.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                MyApp.mFinishNumRun++;
            }
            if (!this.exit) {
                EventBus.getDefault().post(new DoAutoBean(104, null, null, 0));
            }
        } else {
            if (this.pause) {
                onPause();
            }
            SDK.allNum = 1;
            SDK.nowNum = 1;
            DoActionUtils.doAction(this, this.mActionBean);
            if (!this.exit) {
                EventBus.getDefault().post(new DoAutoBean(104, null, null, 0));
            }
        }
    }

    private void sortList(List<ActionBean> list) {
        Collections.sort(list, new Comparator<ActionBean>() {
            public int compare(ActionBean actionBean, ActionBean actionBean2) {
                if (actionBean.getSortNum() > actionBean2.getSortNum()) {
                    return 1;
                }
                return actionBean.getSortNum() == actionBean2.getSortNum() ? 0 : -1;
            }
        });
    }
}
